/**
 * @name        locales.ts
 * @author      shenyc
 * @date        2020-05-06 - ?
 * @copyright   Copy right (c) shenyc (shenyczz@163.com).
 *              All rights reserved.
 *
 ******************************************************************************/
// {{@@@}}
"use strict"
// ----------------------------------------------------------------------------
/**
 * vueI18n 国际化
 * @author shenyc
 */
import { createI18n } from "vue-i18n"
import { defaultStorage, StoreID } from "@/stores"

// 自定义语言包
import enCustom from "./languages/en"
import cnCustom from "./languages/zh-CN"

const messages = {
    en: {
        langName: "English",
        ...enCustom,
    },
    "zh-CN": {
        langName: "简体中文",
        ...cnCustom,
    },
}

// 默认读取本地存储语言设置
const language_key = () => {
    let result = "en" as LanguageKey
    const locale_state = defaultStorage().getItem(StoreID.LOCALE_STORE)
    if (locale_state) {
        const jdata = JSON.parse(locale_state) as Partial<LocaleState>
        result = jdata.languageKey ?? ("en" as LanguageKey)
        // console.log(`result = ${result}`)
        // console.log(defaultLocaleStore)
    }
    return result
}

/**
 * i18n = createI18n(...)
 */
export const i18n = createI18n({
    legacy: false, // 如果要支持 composition API，此项必须设置为 false
    locale: language_key(), // 设置默认语言类型
    fallbackLocale: "en", // 不存在默认则为英文
    fallbackWarn: false, // 关闭警告信息 - [intlify] Fall back to translate 'common.home' key with 'en' locale.
    missingWarn: false, // 关闭警告信息 - [intlify] Not found 'common.home' key in 'en' locale messages.
    globalInjection: true, // true 注册$t方法到Vue组件,可以在组件中直接使用 {{ $t('common.home') }}
    messages: messages, //
})

export default i18n
// ----------------------------------------------------------------------------
// {{@@@}}
